using System;
using SqlSugar;
using System.Collections.Generic;
using Infrastructure;
using Infrastructure.Attribute;
using ZR.Model;
using ZR.Model.Dto;
using ZR.Model.Models;
using ZR.Repository;
using ZR.Service.Resourcemng.IResourcemngService;

namespace ZR.Service.Resourcemng
{
    /// <summary>
    /// 资源管理Service业务层处理
    ///
    /// @author admin
    /// @date 2022-08-13
    /// </summary>
    [AppService(ServiceType = typeof(IResourceService), ServiceLifetime = LifeTime.Transient)]
    public class ResourceService : BaseService<Resource>, IResourceService
    {
        private readonly ResourceRepository _ResourceRepository;
        public ResourceService(ResourceRepository repository)
        {
            _ResourceRepository = repository;
        }

        #region 业务逻辑代码

        /// <summary>
        /// 查询资源管理列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<Resource> GetList(ResourceQueryDto parm)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<Resource>();

            //搜索条件查询语法参考Sqlsugar
            predicate = predicate.AndIF(parm.Id != null, it => it.Id == parm.Id);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Name), it => it.Name.Contains(parm.Name));
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Path), it => it.Path.Contains(parm.Path));
            predicate = predicate.AndIF(parm.Version != null, it => it.Version == parm.Version);
            var response = _ResourceRepository
                .Queryable()
                .Where(predicate.ToExpression())
                .ToPage(parm);

            return response;
        }

        /// <summary>
        /// 添加资源管理
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public int AddResource(Resource parm)
        {
            var response = _ResourceRepository.Insert(parm, it => new
            {
                it.Name,
                it.Description,
                it.FileType,
                it.Path,
                it.PathAndroid,
                it.PathIOS,
                it.PathPC,
                it.Version,
            });
            return response;
        }

        /// <summary>
        /// 修改资源管理
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public int UpdateResource(Resource parm)
        {
            var response = _ResourceRepository.Update(w => w.Id == parm.Id, it => new Resource()
            {
                Name = parm.Name,
                Description = parm.Description,
                FileType = parm.FileType,
                Path = parm.Path,
                PathAndroid = parm.PathAndroid,
                PathIOS = parm.PathIOS,
                PathPC = parm.PathPC,
                Version = parm.Version,
            });
            return response;
        }
        #endregion
    }
}